 /*   
 * Project: OSMP
 * FileName: DeviceServiceImpl.java
 * version: V1.0
 */
package com.cnpc.iot.scene.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnpc.framework.base.dao.RedisDao;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.iot.scene.entity.Metrics;
import com.cnpc.iot.scene.entity.Sensor;
import com.cnpc.iot.scene.service.DeviceService;
import com.cnpc.iot.scene.vo.SensorVo;

/**
 * Description:
 * @author: wangkaiping
 * @date: 2018年3月15日 下午2:01:34上午10:51:30
 */
@Service
public class DeviceServiceImpl implements DeviceService{

	private String REDIS_COLL_KEY = "DEVICEDATA:";
	
	@Resource
	private RedisDao redisDao;
	@Resource
    private BaseService baseService;
	
	@Override
	public List<SensorVo> getMetric(String deviceId) {
		String hql = "from Sensor where deviceId=:deviceId";
        Map<String, Object> params = new HashMap<>();
        params.put("deviceId", deviceId);
        List<Sensor> sensors = baseService.find(hql, params);
        if(null != sensors && sensors.size() > 0){
        	List<SensorVo> list = new ArrayList<>();
        	List<Metrics> metrics = redisDao.getList(REDIS_COLL_KEY+deviceId, Metrics.class);
        	for(Sensor i : sensors){
        		SensorVo vo = new SensorVo();
        		vo.setDeviceId(i.getDeviceId());
        		vo.setIcon(i.getIcon());
        		vo.setName(i.getType().getName());
        		vo.setSensorId(i.getId());
        		vo.setUnit(i.getUnit());
        		vo.setValues("Na");
        		if(null != metrics && metrics.size() > 0){
            		for(Metrics k : metrics){
            			if(i.getId().equals(k.getTags().get("scode")) ){
            				vo.setValues(String.valueOf(k.getValue()));
            			}
            		}
            	}
        		list.add(vo);
        	}
        	return list;
        }
		return null;
	}

}
